package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.BondTransferParData;
import com.kkd.kkdapp.http.entity.CancelDebtData;
import com.kkd.kkdapp.http.entity.Tags;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.UIEmptyHandleUtil;
import com.kkd.kkdapp.view.CustomScrollView;
import com.kkd.kkdapp.view.KKdWebview;
import com.kkd.kkdapp.view.CustomScrollView.OnRefreshListener;
import com.kkd.kkdapp.view.CustomScrollView.OnScrollListener;
import com.kkd.kkdapp.view.MyProgressbar;
import com.kkd.kkdapp.view.MyRelativeLayout.OnItemClick;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/*******
 * 
 * @Name 债权标的详情
 * 
 * @author mty
 * 
 */
public class CreditorIsRightsParticularsActivity extends BaseActivity implements
		OnClickListener {

	private static final int DETAILS_REQUEST_SUCCESS = 1;
	private static final int DETAILS_REQUEST_FAIL = 2;
	private static final int WITHDRAW_CODE_ZERO = 3;
	private static final int WITHDRAW_CODE_ELSE = 4;
	private static final int PWU_CODE_ZERO = 5;
	private static final int PWU_CODE_ELSE = 6;

	private RelativeLayout title_img; // 后退键
	private RelativeLayout particulars_title; // 标题栏
	private CustomScrollView mScrollView; // 下拉刷新
	private int centralHeight;
	private LinearLayout central;
	private String[] imaIdArray; // icon-img数组
	private String[] textArray; // icon-str数组
	private ImageView icon_img1, icon_img2, icon_img3, icon_img4; // icon-img
	private TextView icon_tv1, icon_tv2, icon_tv3, icon_tv4; // icon-str
	private RelativeLayout icon_rl1, icon_rl2, icon_rl3, icon_rl4; // icon
	private Dialog viewPagerDialog;
	private RelativeLayout project_course; // 项目历程
	private KKdWebview webview;
	private RelativeLayout counter_rl; // 计算器
	private Dialog oneDialog; // 第一次进入K计划详情弹窗
	private Button tender_bt; // 投标按钮
	private List<Tags> tags;
	private double balance; // 账户余额
	private TextView title_tv; // 标题
	private TextView eachPrice_tv; // 每份价格
	private TextView expected_tv1, expected_tv2, expected_tv3, expected_tv4; // 预期年化收益率
	private TextView term_tv1, term_tv2, term_tv3, term_tv4; // 剩余期限
	private TextView transfer_tv; // 转让系数
	private TextView collect_tv; // 待收本金
	private DecimalFormat df = new DecimalFormat("0.01");
	private DecimalFormat df1 = new DecimalFormat("0.1");
	private TextView purchase_amount; // 剩余份数
	private MyProgressbar progressbar; // 进度条
	private Timer timer = null;
	private TimerTask myTimerTask = null;
	private int progress = 0;
	private double iinterest = 0.00;
	private TextView purchase_schedule_tv; // 购买进度
	private String projectHistory; // 项目历程
	private boolean webFlag = true;
	private String poiId = "";
	private long perAmount; // 每份价格
	private int leftCount; // 剩余份数
	private long perInterest; // 每份收益
	private boolean firstEntryFlag = true;
	private RelativeLayout par_ll;
	private RelativeLayout rel_no_content;
	private LinearLayout rel_content;
	private ImageView img_null;
	private TextView tv_null;
	private BondTransferParData data;
	private Gson gson = new Gson();
	private Dialog withdrawDialog; // 撤回剩余债权Dialog
	private Dialog withdrawSuccessDialog;
	private Dialog loadDialog;
	private String transferId;
	private Dialog videoDialog;
	private TextView eachPrice_company; // 每份金额单位
	private long perPrincipal;
	private long leftTerm;
	private long preYearRate;
	private long activityRate;
	private long perPrice;

	Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case DETAILS_REQUEST_SUCCESS:
				mScrollView.onRefreshComplete();
				data = (BondTransferParData) msg.obj;
				perAmount = data.getPerAmount();
				leftCount = data.getLeftCount();
				perInterest = data.getPerInterest();
				perPrincipal = data.getPerPrincipal();
				leftTerm = data.getLeftTerm();
				preYearRate = data.getPreYearRate();
				activityRate = data.getActivityRate();
				perPrice = data.getPerPrice();
				tags = data.getTags(); // icon数组
				imgIconHandle(tags); // icon加载
				if (PreferenceUtils.getInstance()
						.getOneCreditorIsRightsParticularsDialog(
								CreditorIsRightsParticularsActivity.this)) {
					oneFinancialParticularsDialog();
					PreferenceUtils.getInstance()
							.setOneCreditorIsRightsParticularsDialog(
									CreditorIsRightsParticularsActivity.this,
									false);
				}
				dataLoading(data); // 数据加载
				break;
			case DETAILS_REQUEST_FAIL:
				int statusCode = (Integer) msg.obj;
				if (firstEntryFlag) {
					par_ll.setVisibility(View.GONE);
				}
				LogUtil.e("statusCode---->", statusCode + "");
				if (statusCode == KkdHttpClient.NO_NET) {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							CreditorIsRightsParticularsActivity.this,
							firstEntryFlag, true, rel_content, rel_no_content,
							img_null, tv_null, "网络错误，请稍后重试",
							R.drawable.img_null_net);
				} else {
					firstEntryFlag = UIEmptyHandleUtil.uiNullDispose(
							CreditorIsRightsParticularsActivity.this,
							firstEntryFlag, true, rel_content, rel_no_content,
							img_null, tv_null, "服务异常，请稍后重试",
							R.drawable.img_null_net);
				}
				mScrollView.onRefreshComplete();
				break;
			case WITHDRAW_CODE_ZERO:
				withdrawTransferSuccessDialog();
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				break;
			case WITHDRAW_CODE_ELSE:
				String descStr = (String) msg.obj;
				showToast(descStr);
				if (loadDialog != null) {
					loadDialog.dismiss();
				}
				break;
			case PWU_CODE_ZERO:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				CancelDebtData cancelDebtData = (CancelDebtData) msg.obj;
				if (cancelDebtData != null) {
					int remainParts = cancelDebtData.getRemainParts();
					double waitCollectAmount = cancelDebtData
							.getWaitCollectAmount();
					withdrawTransferDialog(remainParts + "份",
							df.format(waitCollectAmount * 0.01) + "元");
				}
				break;
			case PWU_CODE_ELSE:
				if (loadDialog != null) {
					loadDialog.cancel();
					loadDialog = null;
				}
				String desc = (String) msg.obj;
				if (!TextUtils.isEmpty(desc)) {
					AppUtil.makeToastForCustomer(
							CreditorIsRightsParticularsActivity.this, desc);
				}
				break;
			default:
				break;
			}
		};
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (progress != 0) {
				if (progress == 99) {
					progressbar.setProgress(100);
				} else {
					progressbar.setProgress(progress);
				}
			} else {
				progressbar.setProgress((int) iinterest);
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creditor_is_rights_particulars);
		mTintManager.setStatusBarTintResource(0x00000000);
		initView();
		setListener();
	}

	/***
	 * 初始化控件
	 */
	public void initView() {
		title_img = (RelativeLayout) this.findViewById(R.id.title_img);
		particulars_title = (RelativeLayout) this
				.findViewById(R.id.particulars_title);
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
			LayoutParams params = particulars_title.getLayoutParams();
			params.height = SystemUtils.dip2px(this, 50);
			particulars_title.setLayoutParams(params);
		}
		title_tv = (TextView) this.findViewById(R.id.title_tv);
		eachPrice_tv = (TextView) this.findViewById(R.id.eachPrice_tv);
		expected_tv1 = (TextView) this.findViewById(R.id.expected_tv1);
		expected_tv2 = (TextView) this.findViewById(R.id.expected_tv2);
		expected_tv3 = (TextView) this.findViewById(R.id.expected_tv3);
		expected_tv4 = (TextView) this.findViewById(R.id.expected_tv4);
		term_tv1 = (TextView) this.findViewById(R.id.term_tv1);
		term_tv2 = (TextView) this.findViewById(R.id.term_tv2);
		term_tv3 = (TextView) this.findViewById(R.id.term_tv3);
		term_tv4 = (TextView) this.findViewById(R.id.term_tv4);
		transfer_tv = (TextView) this.findViewById(R.id.transfer_tv);
		progressbar = (MyProgressbar) this.findViewById(R.id.progressbar);
		purchase_schedule_tv = (TextView) this
				.findViewById(R.id.purchase_schedule_tv);
		collect_tv = (TextView) this.findViewById(R.id.collect_tv);

		par_ll = (RelativeLayout) this.findViewById(R.id.par_ll);
		rel_no_content = (RelativeLayout) this
				.findViewById(R.id.rel_no_content);
		rel_content = (LinearLayout) this.findViewById(R.id.rel_content);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);
		eachPrice_company = (TextView) this
				.findViewById(R.id.eachPrice_company);

		purchase_amount = (TextView) this.findViewById(R.id.purchase_amount);
		central = (LinearLayout) this.findViewById(R.id.central);
		mScrollView = (CustomScrollView) this.findViewById(R.id.pt_sv);
		mScrollView.setHeaderBackgroundResource(R.color.blue_4845a4);
		mScrollView.setArrowImageView(R.drawable.xlistview_arrow_white);
		mScrollView.setGifDrawable(R.drawable.four_white_dot2);

		icon_img1 = (ImageView) this.findViewById(R.id.icon_img1);
		icon_img2 = (ImageView) this.findViewById(R.id.icon_img2);
		icon_img3 = (ImageView) this.findViewById(R.id.icon_img3);
		icon_img4 = (ImageView) this.findViewById(R.id.icon_img4);
		icon_tv1 = (TextView) this.findViewById(R.id.icon_tv1);
		icon_tv2 = (TextView) this.findViewById(R.id.icon_tv2);
		icon_tv3 = (TextView) this.findViewById(R.id.icon_tv3);
		icon_tv4 = (TextView) this.findViewById(R.id.icon_tv4);
		icon_rl1 = (RelativeLayout) this.findViewById(R.id.icon_rl1);
		icon_rl2 = (RelativeLayout) this.findViewById(R.id.icon_rl2);
		icon_rl3 = (RelativeLayout) this.findViewById(R.id.icon_rl3);
		icon_rl4 = (RelativeLayout) this.findViewById(R.id.icon_rl4);

		project_course = (RelativeLayout) this
				.findViewById(R.id.project_course);

		webview = (KKdWebview) this.findViewById(R.id.webview);
		webview.setFocusable(true);
		WebSettings webSettings = webview.getSettings();
		// 设置WebView属性，能够执行Javascript脚本
		webSettings.setJavaScriptEnabled(true);
		webview.getSettings().setDefaultTextEncodingName("utf-8");
		// 设置可以访问文件
		webSettings.setAllowFileAccess(true);
		// 设置支持缩放
		webSettings.setBuiltInZoomControls(true);
		webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
		webview.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view,
					final String url) {
				if (url.startsWith("tel:")) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri
							.parse(url));
					startActivity(intent);
				} else if (url.startsWith("http:") || url.startsWith("https:")) {
					if (url.indexOf("video") > 0
							&& !SystemUtils.getNetTypeName().equals("WIFI")) {

						if (videoDialog != null) {
							videoDialog.cancel();
							videoDialog = null;
						}
						videoDialog = DialogUtils.generate2BDialog(
								CreditorIsRightsParticularsActivity.this,
								"您处于非WIFI环境,即将使用流量查看视频,是否继续?", "确定", "取消",
								new OnClickListener() {

									@Override
									public void onClick(View v) {
										videoDialog.dismiss();
										if (!TextUtils.isEmpty(url)) {
											Intent intent = new Intent(
													CreditorIsRightsParticularsActivity.this,
													KKDWebActivity.class);
											intent.putExtra("url", url);
											// intent.putExtra("titleName",
											// bidTitle);
											startActivity(intent);
										}
									}
								}, new OnClickListener() {

									@Override
									public void onClick(View v) {
										videoDialog.dismiss();

									}
								});
						videoDialog.show();

					} else {
						if (!TextUtils.isEmpty(url)) {
							Intent intent = new Intent(
									CreditorIsRightsParticularsActivity.this,
									KKDWebActivity.class);
							intent.putExtra("url", url);
							// intent.putExtra("titleName", bidTitle);
							startActivity(intent);
						}
					}
				}
				return true;
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
			};

			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				handler.proceed();
			}
		});

		counter_rl = (RelativeLayout) this.findViewById(R.id.counter_rl);
		tender_bt = (Button) this.findViewById(R.id.tender_bt);
	}

	/***
	 * 注册监听事件
	 */
	public void setListener() {
		title_img.setOnClickListener(this);
		icon_rl1.setOnClickListener(this);
		icon_rl2.setOnClickListener(this);
		icon_rl3.setOnClickListener(this);
		icon_rl4.setOnClickListener(this);
		project_course.setOnClickListener(this);
		counter_rl.setOnClickListener(this);
		tender_bt.setOnClickListener(this);
		mScrollView.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				webFlag = true;
				requestParticulars();
				if (PreferenceUtils.getInstance().getLogin(
						CreditorIsRightsParticularsActivity.this) == 0) {
					getAccountInfo();
				}
			}
		});
		mScrollView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScroll(int scrollY) {
				if (scrollY >= centralHeight) {
					particulars_title.setBackgroundColor(Color
							.parseColor("#4845a4"));
				} else {
					particulars_title.setBackgroundResource(R.drawable.top_bg);
				}
			}
		});
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		centralHeight = central.getHeight();
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_img: // 后退事件
			finish();
			break;
		case R.id.icon_rl1: // icon
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(1, imaIdArray, textArray);
			}
			break;
		case R.id.icon_rl2:
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(2, imaIdArray, textArray);
			}
			break;
		case R.id.icon_rl3:
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(3, imaIdArray, textArray);
			}
			break;
		case R.id.icon_rl4:
			if (imaIdArray != null && imaIdArray.length > 0) {
				showViewPagerDialog(4, imaIdArray, textArray);
			}
			break;
		case R.id.project_course: // 项目历程
			SystemUtils.onEvent("Regular_Detail_Process", 1);
			if (!TextUtils.isEmpty(projectHistory)) {
				intent = new Intent(this, KKDWebActivity.class);
				intent.putExtra("url", projectHistory);
				intent.putExtra("titleName", "项目历程");
				startActivity(intent);
			}
			break;
		case R.id.counter_rl: // 计算器
			SystemUtils.onEvent("Regular_Detail_Calculator", 1);
			if (AppUtil.isConnect(this)) {
				DialogUtils.showCalculateWindow(perPrincipal, leftTerm,
						preYearRate, activityRate, perPrice, perAmount,
						leftCount, this, v);
				softKeyboard();
			}
			break;
		case R.id.tender_bt: // 立即购买按钮||撤回剩余债权
			if (data != null) {
				int isOwner = data.getIsOwner();
				if (isOwner == 0) {
					tenderButton();
				} else if (isOwner == 1) {
					preWithdrawalUpdate();
				}
			}
			break;
		default:
			break;

		}
	}

	/*******
	 * 投标button跳转
	 *******/
	public void tenderButton() {
		Intent intent;
		String json = gson.toJson(data);
		if (PreferenceUtils.getInstance().getLogin(this) == 0) { // 登陆验证

			if (PreferenceUtils.getInstance().getAutonym(this) == 1) { // 身份认证

				if (PreferenceUtils.getInstance().getBoundBankCard(
						CreditorIsRightsParticularsActivity.this) == 1) { // 判定银行卡是否绑定
					intent = new Intent(this,
							CreditorIsRightsTenderAffirmActivity.class);
					intent.putExtra("json", json);
					this.startActivity(intent);
				} else {
					if (balance == 0) {
						intent = new Intent(this, ForTenderActivity.class);
						intent.putExtra("json", json);
						intent.putExtra("enterTypeFlag", 6);
						startActivity(intent);
					} else {
						intent = new Intent(this,
								CreditorIsRightsTenderAffirmActivity.class);
						intent.putExtra("json", json);
						this.startActivity(intent);
					}

				}

			} else {
				intent = new Intent(this, ForTenderActivity.class);
				intent.putExtra("json", json);
				intent.putExtra("enterTypeFlag", 6);
				this.startActivity(intent);
			}

		} else {
			intent = new Intent(this, LoginActivity.class);
			this.startActivity(intent);

		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		getMessage();
	}

	/***********************
	 * 获取上个界面传过来的消息
	 ***********************/
	public void getMessage() {
		Intent intent = this.getIntent();
		transferId = intent.getStringExtra("transferId");
		poiId = intent.getStringExtra("poiId");
		requestParticulars();
		if (PreferenceUtils.getInstance().getLogin(this) == 0) {
			getAccountInfo();
		}

	}

	/*****************
	 * 请求定期详情接口
	 ****************/
	public void requestParticulars() {
		KkdHttpMagager.getInstance().BondTransferParticulars(transferId,
				BondTransferParData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						BondTransferParData data = (BondTransferParData) responseBody;
						Message msg = new Message();
						msg.what = DETAILS_REQUEST_SUCCESS;
						msg.obj = data;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = DETAILS_REQUEST_FAIL;
						msg.obj = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	/***
	 * 数据加载
	 */
	public void dataLoading(BondTransferParData data) {
		// 标题
		String title = data.getTitle();
		title_tv.setText(title);
		// 每份价格
		long perAmount = data.getPerAmount();
		eachPrice_tv.setText(df.format(perAmount * 0.01));
		// 预期年化收益率
		String perYearRateDisplay = data.getPerYearRateDisplay();
		String activityRateDisplay = data.getActivityRateDisplay();
		if (activityRateDisplay != null && !"0".equals(activityRateDisplay)) {
			expected_tv1.setText(perYearRateDisplay);
			expected_tv2.setText("%+");
			expected_tv3.setText(activityRateDisplay);
			expected_tv4.setText("%");
		} else {
			expected_tv1.setText(perYearRateDisplay);
			expected_tv2.setText("%");
			expected_tv3.setText("");
			expected_tv4.setText("");
		}
		// 剩余期限
		int leftTermMont = data.getLeftTermMonthDesc();
		int leftTermDayDesc = data.getLeftTermDayDesc();
		if (leftTermMont != 0 && leftTermDayDesc != 0) {
			term_tv1.setText(leftTermMont + "");
			term_tv2.setText("个月");
			term_tv3.setText(leftTermDayDesc + "");
			term_tv4.setText("天");
		} else if (leftTermMont == 0) {
			term_tv1.setText(leftTermDayDesc + "");
			term_tv2.setText("天");
			term_tv3.setText("");
			term_tv4.setText("");
		} else if (leftTermDayDesc == 0) {
			term_tv1.setText(leftTermMont + "");
			term_tv2.setText("个月");
			term_tv3.setText("");
			term_tv4.setText("");
		} else if (leftTermMont == 0 && leftTermDayDesc == 0) {
			term_tv1.setText(leftTermDayDesc + "");
			term_tv2.setText("天");
			term_tv3.setText("");
			term_tv4.setText("");
		}
		// 转让系数
		long transferRatio = data.getTransferRatio();
		transfer_tv.setText(df1.format(transferRatio * 0.01) + "%");
		// 待收本息
		long leftIncome = data.getLeftIncome();
		collect_tv.setText(df.format(leftIncome * 0.01) + "元");
		// 进度条
		String transferProgress = data.getTransferProgress();
		if (!"".equals(transferProgress))
			iinterest = Double.parseDouble(transferProgress);
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (myTimerTask != null) {
			myTimerTask.cancel();
			myTimerTask = null;
		}

		if (timer == null)
			timer = new Timer();
		if (myTimerTask == null) {
			myTimerTask = new TimerTask() {

				@Override
				public void run() {
					progress = progress + 1;
					if (progress <= iinterest) {
						handler.sendEmptyMessage(1);
					} else {
						if (iinterest < 1 && iinterest > 0) {
							progressbar.setProgress(1);
						}
						progress = 0;

						myTimerTask.cancel();
						myTimerTask = null;
						timer.cancel();
						timer = null;
					}

				}
			};
		}
		timer.schedule(myTimerTask, 00, 10);
		// 剩余份数
		int leftCount = data.getLeftCount();
		purchase_amount.setText("" + leftCount);
		// 购买进度
		purchase_schedule_tv.setText(transferProgress + "%");
		// 项目历程
		projectHistory = data.getProjectHistory();
		// webView
		String appdebtsdetail = data.getAppdebtsdetail();
		if (webFlag) {
			webFlag = false;
			// h5url
			LogUtil.e("appdebtsdetail", appdebtsdetail);

			if (PreferenceUtils.getInstance().getLogin(
					CreditorIsRightsParticularsActivity.this) == 0) {
				String customerIdAndToken = PreferenceUtils
						.getInstance()
						.getcustomerId(CreditorIsRightsParticularsActivity.this);
				String[] cat = customerIdAndToken.split("&");
				webview.loadUrl(appdebtsdetail + "&poiId=" + poiId
						+ "&customerId=" + cat[1] + "&token=" + cat[0]);
			} else {
				webview.loadUrl(appdebtsdetail + "&poiId=" + poiId
						+ "&customerId=&token=");
			}
		}
		// 转让状态
		int transferStatus = data.getTransferStatus();
		if (transferStatus == 10) { // 转让中
			// 是否为债权拥有者 isOwner
			int isOwner = data.getIsOwner();
			tender_bt.setEnabled(true);
			tender_bt.setClickable(true);
			if (isOwner == 0) {
				tender_bt.setText("立即购买");
				tender_bt
						.setBackgroundResource(R.drawable.particulars_tender_selector);
			} else if (isOwner == 1) {
				tender_bt.setBackgroundResource(R.drawable.withdraw_selector);
				tender_bt.setText("撤回剩余债权");
			}
			eachPrice_company.setVisibility(View.VISIBLE);

		} else if (transferStatus == 20) { // 已转让
			eachPrice_tv.setText("— —");
			tender_bt.setEnabled(false);
			tender_bt.setClickable(false);
			tender_bt.setText("已转让");
			term_tv1.setText("0");
			term_tv2.setText("天");
			term_tv3.setText("");
			term_tv4.setText("");
			eachPrice_company.setVisibility(View.GONE);

		} else if (transferStatus == 30) { // 已结束
			eachPrice_tv.setText("— —");
			tender_bt.setEnabled(false);
			tender_bt.setClickable(false);
			tender_bt.setText("已结束");
			term_tv1.setText("0");
			term_tv2.setText("天");
			term_tv3.setText("");
			term_tv4.setText("");
			eachPrice_company.setVisibility(View.GONE);

		}
	}

	/***
	 * icon加载
	 */
	public void imgIconHandle(List<Tags> tags) {
		if (tags.size() == 1) {
			icon_rl1.setVisibility(View.VISIBLE);
		} else if (tags.size() == 2) {
			icon_rl1.setVisibility(View.VISIBLE);
			icon_rl2.setVisibility(View.VISIBLE);
		} else if (tags.size() == 3) {
			icon_rl1.setVisibility(View.VISIBLE);
			icon_rl2.setVisibility(View.VISIBLE);
			icon_rl3.setVisibility(View.VISIBLE);
		} else if (tags.size() == 4) {
			icon_rl1.setVisibility(View.VISIBLE);
			icon_rl2.setVisibility(View.VISIBLE);
			icon_rl3.setVisibility(View.VISIBLE);
			icon_rl4.setVisibility(View.VISIBLE);
		}
		imaIdArray = new String[tags.size()];
		textArray = new String[tags.size()];
		for (int i = 0; i < tags.size(); i++) {
			imaIdArray[i] = tags.get(i).getImageBig();
			textArray[i] = tags.get(i).getNameBig();
			if (i == 0) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img1, R.drawable.mybank_default);
				icon_tv1.setText(tags.get(i).getName());
			} else if (i == 1) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img2, R.drawable.mybank_default);
				icon_tv2.setText(tags.get(i).getName());
			} else if (i == 2) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img3, R.drawable.mybank_default);
				icon_tv3.setText(tags.get(i).getName());
			} else if (i == 3) {
				ImageLoader.getInstance().displayImage(tags.get(i).getImage(),
						icon_img4, R.drawable.mybank_default);
				icon_tv4.setText(tags.get(i).getName());
			}
		}
	}

	/**
	 * 
	 * 查询账户余额
	 */
	public void getAccountInfo() {

		KkdHttpMagager.getInstance().getAccountInfoManager(String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						try {
							String responseString = responseBody.toString();
							JSONObject data = new JSONObject(responseString);
							balance = data.getDouble("balance");
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
					}
				});
	}

	/***
	 * 撤回剩余债权接口
	 */
	public void requsetCancelDebtTrans() {
		loadDialog = DialogUtils.toLoadDialog(this, "债权撤回中，请耐心等待...");
		KkdHttpMagager.getInstance().getCancelDebtTrans(data.getTransferId(),
				String.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						mHandler.sendEmptyMessage(WITHDRAW_CODE_ZERO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = desc;
						msg.what = WITHDRAW_CODE_ELSE;
						mHandler.sendMessage(msg);
					}
				});
	}

	/*****
	 * icon弹窗
	 * 
	 * @param currentItem
	 * @param imaIdArray
	 * @param textArray
	 */
	private void showViewPagerDialog(int currentItem,
			final String[] imaIdArray, String[] textArray) {
		if (viewPagerDialog != null) {
			viewPagerDialog.cancel();
			viewPagerDialog = null;
		}
		viewPagerDialog = DialogUtils.showCharacteristicDialog(
				CreditorIsRightsParticularsActivity.this, currentItem,
				imaIdArray, textArray, new OnItemClick() {

					@Override
					public void getPosition(int position) {
						if (tags != null && tags.size() != 0
								&& "安全保障".equals(tags.get(position).getName())) {
							Intent intent = new Intent(
									CreditorIsRightsParticularsActivity.this,
									KKDWebActivity.class);
							intent.putExtra("url",
									com.kkd.kkdapp.Environment.getH5Url()
											+ "appinsurance.html"); // 正式
							intent.putExtra("titleName", "安全保障");
							startActivity(intent);
							if (viewPagerDialog != null) {
								viewPagerDialog.cancel();
								viewPagerDialog = null;
							}
						}
					}
				});
		viewPagerDialog.show();
	}

	public void softKeyboard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		// 得到InputMethodManager的实例
		if (imm.isActive()) {
			// 如果开启
			imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
					InputMethodManager.HIDE_NOT_ALWAYS);
			// 关闭软键盘，开启方法相同，这个方法是切换开启与关闭状态的
		}
	}

	// 第一次进入债权详情界面的Dialog
	public void oneFinancialParticularsDialog() {
		if (oneDialog != null) {
			oneDialog.cancel();
			oneDialog = null;
		}
		oneDialog = DialogUtils.oneFinancialParticularsDialog(this,
				R.drawable.cr_onedialog1, R.drawable.cr_onedialog2);
		oneDialog.show();
	}

	// 撤回前更新数据
	public void preWithdrawalUpdate() {
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
		loadDialog = DialogUtils.toLoadDialog(this, "加载中...");
		KkdHttpMagager.getInstance().getCancelDebtTransData(
				data.getTransferId(), CancelDebtData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						CancelDebtData cancelDebtData = (CancelDebtData) responseBody;
						Message msg = new Message();
						msg.obj = cancelDebtData;
						msg.what = PWU_CODE_ZERO;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.obj = desc;
						msg.what = PWU_CODE_ELSE;
						mHandler.sendMessage(msg);
					}
				});
	}

	// 撤回剩余债权弹窗
	public void withdrawTransferDialog(String leftCount, String perIncome) {

		if (withdrawDialog != null) {
			withdrawDialog.cancel();
			withdrawDialog = null;
		}
		withdrawDialog = DialogUtils.cancelDebt(this, leftCount, perIncome,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						switch (v.getId()) {
						case R.id.tv_sure:
							if (withdrawDialog != null) {
								withdrawDialog.dismiss();
							}
							requsetCancelDebtTrans();
							break;
						}

					}
				});
		withdrawDialog.show();
	}

	// 撤回剩余债权成功弹窗
	public void withdrawTransferSuccessDialog() {
		if (withdrawSuccessDialog != null) {
			withdrawSuccessDialog.cancel();
			withdrawSuccessDialog = null;
		}
		withdrawSuccessDialog = DialogUtils.cancelDebtSuccess(this,
				data.getTitle(), new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (withdrawSuccessDialog != null) {
							withdrawSuccessDialog.dismiss();
						}
						finish();
					}
				});
		withdrawSuccessDialog.show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myTimerTask != null) {
			myTimerTask.cancel();
			myTimerTask = null;
		}
		if (timer != null) {
			timer.cancel();
			timer = null;
		}

		if (oneDialog != null) {
			oneDialog.cancel();
			oneDialog = null;
		}
		if (viewPagerDialog != null) {
			viewPagerDialog.cancel();
			viewPagerDialog = null;
		}
		if (withdrawDialog != null) {
			withdrawDialog.cancel();
			withdrawDialog = null;
		}
		if (withdrawSuccessDialog != null) {
			withdrawSuccessDialog.cancel();
			withdrawSuccessDialog = null;
		}
		if (loadDialog != null) {
			loadDialog.cancel();
			loadDialog = null;
		}
	}
}
